package com.me.bbs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.anno.RequireLogin;
import com.me.bbs.common.AdminType;
import com.me.bbs.common.AuthUtils;
import com.me.bbs.common.Resp;
import com.me.bbs.dto.AdminDTO;
import com.me.bbs.entity.Administrator;
import com.me.bbs.service.IAdministratorService;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lm
 * @since 2023-01-29
 */
@RestController
@RequestMapping("/administrator")
public class AdministratorController {
    @Resource
    private IAdministratorService administratorService;
    @GetMapping("/search")
    public Resp<?> searchUser(@RequestParam("forumId") Long forumId, @RequestParam("q") String q,
                              @RequestParam("current") Long current,
                              @RequestParam("size") Long size) {
        Page<AdminDTO> adminDTOS = administratorService.searchUser(forumId, q,current,size);
        return Resp.success(adminDTOS);
    }

    @GetMapping("/page")
    public Resp<?> page(@RequestParam("forumId") Long forumId,
                              @RequestParam("current") Long current,
                              @RequestParam("size") Long size) {
        Page<AdminDTO> adminDTOS = administratorService.getAdmins(forumId, current,size);
        return Resp.success(adminDTOS);
    }

    @PostMapping("/admin")
    public Resp<?> addAsAdmin(@RequestParam("forumId") Long forumId, @RequestParam("userId") Long userId) {
        AuthUtils.checkAdmin(AdminType.OWNER,forumId);
        administratorService.addAdmin(forumId, userId);
        return Resp.success();
    }
    @DeleteMapping("/admin")
    public Resp<?> deleteAdmin(@RequestParam("forumId") Long forumId, @RequestParam("userId") Long userId) {
        AuthUtils.checkAdmin(AdminType.OWNER,forumId);
        administratorService.lambdaUpdate().eq(Administrator::getForum, forumId).eq(Administrator::getUser,userId)
                .remove();
        return Resp.success();
    }

    @GetMapping("/getAuthInfo")
    @RequireLogin
    public Resp<?> getAuthInfo(@RequestParam("forumId") Long forumId) {
        String info = administratorService.getInfo(forumId);
        return Resp.success(info);
    }
}
